import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wallpaper_app/components/windows/window_title_bar.dart';
import 'package:wallpaper_app/tools/update_apk.dart';

class UpdatePage extends StatefulWidget {
  const UpdatePage({super.key});

  @override
  State<UpdatePage> createState() => _UpdatePageState();
}

class _UpdatePageState extends State<UpdatePage> {
  String version = '';

  void getVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      version = packageInfo.version;
    });
  }

  void superResolution() async {
    launchUrl(Uri.parse(
        'https://gitee.com/zsnoin-can/wallpaper_tools#https://gitee.com/link?target=https%3A%2F%2Fgithub.com%2Fhe55%2FDreamScene2'));
  }

  void officialWebsite() async {
    launchUrl(Uri.parse('https://zsnoin.github.io/bocchi_wallpaper/'));
  }

  void backupWebsite() async {
    launchUrl(Uri.parse('https://boccihi.pages.dev/'));
  }

  @override
  void initState() {
    super.initState();
    getVersion();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    return Scaffold(
      body: Column(
        children: [
          WindowTitleBar(isBack: true),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                spacing: 8,
                children: [
                  Material(
                      color: theme.primaryContainer,
                      borderRadius: BorderRadius.circular(8),
                      clipBehavior: Clip.antiAlias,
                      child: ListTile(
                        title: Text('版本'),
                        trailing: Text('v$version'),
                      )),
                  Material(
                      color: theme.primaryContainer,
                      borderRadius: BorderRadius.circular(8),
                      clipBehavior: Clip.antiAlias,
                      child: ListTile(
                        title: Text('检测更新'),
                        onTap: () {
                          UpdateApk().updateApk(context, displayTips: true);
                        },
                      )),
                  Material(
                      color: theme.primaryContainer,
                      borderRadius: BorderRadius.circular(8),
                      clipBehavior: Clip.antiAlias,
                      child: ListTile(
                        title: Text('官方网站'),
                        subtitle: Text('gitub托管，有时候需要翻墙'),
                        onTap: officialWebsite,
                      )),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
